package com.ctshk.sapp.hotel.APIUtils;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class APIUtil {
    public static String callAPIFunc(String contentType,String pathUrl,String param,String methodType){
        String result = "";
        {
            URL url =null;
            try
            {
                //创建连接
                url = new URL(pathUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(false);
                conn.setRequestMethod(methodType);
                conn.setRequestProperty("Content-Type", contentType);
                conn.connect();
                //参数
                DataOutputStream out =new DataOutputStream(conn.getOutputStream());
                out.writeBytes(param);
                out.flush();
                out.close();
                // 获取响应
                BufferedReader reader =new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String lines;
                StringBuffer sb =new StringBuffer();
                //读取结果
                while
                ((lines = reader.readLine()) !=null){
                    lines =new String(lines.getBytes(),"utf-8");
                    sb.append(lines);
                }
                result = sb.toString();
                // 关闭连接
                reader.close();
                conn.disconnect();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
//    Westminster测试：
    public static void main(String[] args) {
//    String param = "<OTA_HotelDescriptiveInfoRQ xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
//                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Version=\"0\"\n" +
//                "xmlns=\"http://www.opentravel.org/OTA/2003/05\" primaryLangID=\"en-us\">\n" +
//                "<POS>\n" +
//                " <Source>\n" +
//                "\t<RequestorID ID=\"api@ctshk.com\" MessagePassword=\"111111\"/>\n" +
//                " </Source>\n" +
//                "</POS>\n" +
//                "<HotelDescriptiveInfos>\n" +
//                "<HotelDescriptiveInfo HotelCode=\"00001\"/>\n" +
//                "</HotelDescriptiveInfos>\n" +
//                "</OTA_HotelDescriptiveInfoRQ>";

        String param = "<?xml version=\"1.0\"?>\n" +
                "<OTA_HotelResRQ xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
                "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Version=\"0\"\n" +
                "xmlns=\"http://www.opentravel.org/OTA/2003/05\">\n" +
                "<POS>\n" +
                " 30<Source ISOCountry=\"HK\">\n" +
                "1,2<RequestorID ID=\"api@ctshk.com\" MessagePassword=\"111111\"/>\n" +
                " </Source>\n" +
                "</POS>\n" +
                " <HotelReservations>"+
                "<HotelReservation>\n" +
                " 27<UniqueID ID=\"PKG001\" ID_Context=\"XXX\">\n" +
                " <RoomStays>\n" +
                " <RoomStay>\n" +
                " <RoomTypes>\n" +
                " 3,4<RoomType RoomType=\"Standard 2 Beds(Twin)\" RoomTypeCode=\"2@13379_0\" />\n" +
                " </RoomTypes>\n" +
                " <RatePlans>\n" +
                " 5<RatePlan RatePlanID=\"0005-F\" />\n" +
                " </RatePlans>\n" +
                " 6,7<TimeSpan Start=\"2021-03-22\" End=\"2021-03-25\" />\n" +
                " 8<BasicPropertyInfo HotelCode=\"12345\" />\n" +
                " 9<ResGuestRPHs>1 2</ResGuestRPHs>\n" +
                " 28,29<Total AmountBeforeTax=\"18660.0\" CurrencyCode=\"HKD\"/>\n" +
                " </RoomStay>\n" +
                " <RoomStay>\n" +
                " <RoomTypes>\n" +
                " <RoomType RoomType=\"Standard 2 Beds(Twin)\" RoomTypeCode=\"2@13379_0\" />\n" +
                " </RoomTypes>\n" +
                " <RatePlans>\n" +
                " <RatePlan RatePlanID=\"0005-F\" />\n" +
                " </RatePlans>\n" +
                " <TimeSpan Start=\"2021-03-22\" End=\"2021-03-25\" />\n" +
                " <Guarantee>\n" +
                " <GuaranteeDescription>\n" +
                " <Text />\n" +
                " </GuaranteeDescription>\n" +
                " </Guarantee>\n" +
                " <BasicPropertyInfo HotelCode=\"12345\" />\n" +
                " <ResGuestRPHs>3 4</ResGuestRPHs>\n" +
                " <Total AmountBeforeTax=\"18660.0\" CurrencyCode=\"HKD\"/>\n" +
                " </RoomStay>\n" +
                " </RoomStays>\n" +
                " <ResGuests>\n" +
                " 10,11<ResGuest ResGuestRPH=\"1\" PrimaryIndicator=\"true\">\n" +
                " <Profiles>\n" +
                " <ProfileInfo>\n" +
                " <Profile>\n" +
                " <Customer>\n" +
                " <PersonName>\n" +
                " 12<NamePrefix>MR</NamePrefix>\n" +
                " 13<GivenName>KEL01</GivenName>\n" +
                " 14<Surname>TEST ADH</Surname>\n" +
                " </PersonName>\n" +
                " </Customer>\n" +
                " </Profile>\n" +
                " </ProfileInfo>\n" +
                " </Profiles>\n" +
                " <ArrivalTransport>\n" +
                " 15,16,17<TransportInfo Type=\"Flight\" ID=\"0715\" Time=\"2021-03-25T10:29:00\" />\n" +
                " </ArrivalTransport>\n" +
                " <DepartureTransport>\n" +
                " 18,19,20<TransportInfo Type=\"Flight\" ID=\"0716\" Time=\"2021-03-25T10:10:00\" />\n"+
                "</DepartureTransport>\n" +
                " <GuestCounts>\n" +
                " <GuestCount AgeQualifyingCode=\"10\" />\n" +
                " </GuestCounts>\n" +
                " </ResGuest>\n" +
                " <ResGuest ResGuestRPH=\"2\">\n" +
                " <Profiles>\n" +
                " <ProfileInfo>\n" +
                " <Profile>\n" +
                " <Customer>\n" +
                " <PersonName>\n" +
                " <NamePrefix>MR</NamePrefix>\n" +
                " <GivenName>CHILD</GivenName>\n" +
                " <Surname>TEST ADH</Surname>\n" +
                " </PersonName>\n" +
                " </Customer>\n" +
                " </Profile>\n" +
                " </ProfileInfo>\n" +
                " </Profiles>\n" +
                " <ArrivalTransport>\n" +
                " <TransportInfo Type=\"Flight\" ID=\"0715\" Time=\"2021-03-25T10:29:00\" />\n" +
                " </ArrivalTransport>\n" +
                " <DepartureTransport>\n" +
                " <TransportInfo Type=\"Flight\" ID=\"0716\" Time=\"2021-03-25T10:10:00\" />\n" +
                " </DepartureTransport>\n" +
                " <GuestCounts>\n" +
                " 21, 22<GuestCount AgeQualifyingCode=\"8\" Age=\"5\" />\n" +
                " </GuestCounts>\n" +
                " </ResGuest>\n" +
                " <ResGuest ResGuestRPH=\"3\">\n" +
                " <Profiles>\n" +
                " <ProfileInfo>\n" +
                " <Profile>\n" +
                " <Customer>\n" +
                " <PersonName>\n" +
                " <NamePrefix>MR</NamePrefix>\n" +
                " <GivenName>KEL 03</GivenName>\n" +
                " <Surname>TEST ADH</Surname>\n" +
                " </PersonName>\n" +
                " </Customer>\n" +
                " </Profile>\n" +
                " </ProfileInfo>\n" +
                " </Profiles>\n" +
                " <ResGuest ResGuestRPH=\"4\">\n" +
                " <Profiles>\n" +
                " <ProfileInfo>\n" +
                " <Profile>\n" +
                " <Customer>\n" +
                " <PersonName>\n" +
                " <NamePrefix>MR</NamePrefix>\n" +
                " <GivenName>KEL 04</GivenName>\n" +
                " <Surname>TEST ADH</Surname>"+
                "</PersonName>\n" +
                " </Customer>\n" +
                " </Profile>\n" +
                " </ProfileInfo>\n" +
                " </Profiles>\n" +
                " <ArrivalTransport>\n" +
                " <TransportInfo Type=\"Flight\" ID=\"0715\" Time=\"2021-03-25T10:29:00\" />\n" +
                " </ArrivalTransport>\n" +
                " <DepartureTransport>\n" +
                " <TransportInfo Type=\"Flight\" ID=\"0716\" Time=\"2021-03-25T10:10:00\" />\n" +
                " </DepartureTransport>\n" +
                " <GuestCounts>\n" +
                " <GuestCount AgeQualifyingCode=\"10\" />\n" +
                " </GuestCounts>\n" +
                " </ResGuest>\n" +
                " </ResGuests>\n" +
                " <ResGlobalInfo>\n" +
                " <SpecialRequests>\n" +
                " <SpecialRequest>\n" +
                " 23<Text>abc</Text>\n" +
                " </SpecialRequest>\n" +
                " </SpecialRequests>\n" +
                " <Comments>\n" +
                " <Comment>\n" +
                " 31<Text>RqNonSmokingFloor</Text>\n" +
                " <Text>RqDblBed</Text>\n" +
                " <Text>RQEarlyCheckin</Text>\n" +
                " </Comment>\n" +
                " </Comments>\n" +
                " </ResGlobalInfo>\n" +
                " <TPA_Extensions>\n" +
                " <Agent>\n" +
                " 24<ContactPerson>OPH</ContactPerson>\n" +
                " 25<EmailOrFax>pakho.or@hkwtl.com</EmailOrFax>\n" +
                " 26<Phone>23139735</Phone>\n" +
                " </Agent>\n" +
                " </TPA_Extensions>\n" +
                " </HotelReservation>\n" +
                " </HotelReservations>\n" +
                "</OTA_HotelResRQ>\n";
    String post = callAPIFunc("text/xml;charset=utf-8", "http://ws.adholidays.com/hotelapidev/otaservice.aspx", param, "POST");
        System.out.println(post);
//    长隆测试：（返回数据需要解密）
//    String signStr = "9000000242"+"chimelong.trip.park.info.get"+"80c5a87d22ed461bbe91ec691a4d9874"+"2021-03-12 10:13:01"+"1.0"+"{\\\"productCodes\\\":[\\\"G201TCTAF00003_1000000426\\\",\\\"G201BPTAF00024_1000000427\\\",\\\"G201BPTAF00023_1000000427\\\",\\\"G201TDTAF00016_1000000423\\\",\\\"G201TCTAF00004_1000000427\\\",\\\"G201TDTAF00014_1000000423\\\",\\\"G201TDTAF00015_1000000423\\\",\\\"G201TDTAF00017_1000000423\\\",\\\"G201TDTAF00018_1000000423\\\"]}";

//    String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.info.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"productCodes\\\":[\\\"G201BPKAE00002_1000000419\\\"]}}\"}";

//    String bodyStr = "\"notice\": [\n" +"{ \"productCode\": 2222222, \"status\": 0\n" +"}";
//    String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.change.notice\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"notice\\\":[{\\\"productCode\\\":G202TSKAF00147_1000000422,\\\"status\\\":0}]}\"}";
//    String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.product.price.get\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{\\\"productCode\\\":\\\"GGZH002TST0033_1000000418\\\",\\\"begDate\\\":\\\"2021-03-19\\\",\\\"endDate\\\":\\\"2021-03-20\\\"}\"}";
//    System.out.println(CONTENT_WITH_NO_SIGN);
    String CONTENT_WITH_NO_SIGN ="{\"mer_no\":\"9000000242\",\"method\":\"chimelong.hotel.order.create\",\"sign\":\"\",\"nonce_str\":\"80c5a87d22ed461bbe91ec691a4d9874\",\"timestamp\":\"2021-03-12 10:13:01\",\"ver_no\":\"1.0\",\"body\":\"{" +
            "\\\"currency\\\": \\\"HKD\\\"," +
            "\\\"productData\\\": [{" +
            "\\\"productCode\\\": \\\"G202TSTAF00163_1000000418\\\"," +
            "\\\"begDate\\\": \\\"2021-03-22\\\"," +
            "\\\"endDate\\\": \\\"2021-03-23\\\"," +
            "\\\"settlePrice\\\": 29.8," +
            "\\\"quantity\\\": 1," +
            "\\\"dayPriceData\\\": [{" +
            "\\\"roomDate\\\": \\\"2021-03-22\\\"," +
            "\\\"settlePrice\\\": 29.8," +
            "\\\"currency\\\": \\\"HKD\\\"" +
            "}]" +
            "}]" +
            "}\"}";


    String signAppendStr = new APISignUtil().contentJointTest(CONTENT_WITH_NO_SIGN);
    String toData = new APIAesUtil().aesEncodeBase64(signAppendStr, "1d70b9997df4946824810ebb069dbe28");//SIGN_KEY:"1d70b9997df4946824810ebb069dbe28"（长隆提供）
    String params =
            "{" +
                    "\"mer_no\":\"9000000242\","+
                    "\"data\":\""+toData+"\""+
                    "}";
    String contentType = "application/json;charset=utf-8";
    String path = "http://47.244.225.190:8080/hub/product/ota/call2/gsa/chimelong/api.do";
    String returnStr = new APIAesUtil().aesDecodeBase64(callAPIFunc(contentType,path,params,"POST"), "1d70b9997df4946824810ebb069dbe28");
    System.out.println("返回内容解密："+returnStr);
    }
}
